from util.db_util import connect


# 根据条件获取宝宝睡眠记录
def get_baby_sleep_records(baby_id, time):
    try:
        with connect() as conn:
            with conn.cursor() as cursor:
                sql = "SELECT * FROM baby_sleep_records WHERE baby_id = %s AND end_time >= %s"
                params = (baby_id, time)
                print(f"执行的SQL语句： {sql}")
                print(f"参数： {params}")
                cursor.execute(sql, params)
                return cursor.fetchall()
    except Exception as e:
        print(f"发生错误： {e}")
        return None


# 添加宝宝睡眠记录


def add_baby_sleep_record(baby_id, start_time, end_time, duration):
    with connect() as conn:
        with conn.cursor() as cursor:
            # 假设 remark 字段用于存储备注，而不是持续时间
            cursor.execute(
                "INSERT INTO baby_sleep_records (baby_id, start_time, end_time, duration, remark) VALUES (%s, %s, %s, "
                "%s, %s)",
                (baby_id, start_time, end_time, duration, '')  # 假设传入空字符串作为初始备注
            )
        conn.commit()

    # 更新宝宝睡眠记录


def update_baby_sleep_record(record_id, start_time, end_time, duration, remark=''):
    with connect() as conn:
        with conn.cursor() as cursor:
            cursor.execute(
                "UPDATE baby_sleep_records SET start_time = %s, end_time = %s, duration = %s, remark = %s WHERE id = %s",
                (start_time, end_time, duration, remark, record_id)
            )
        conn.commit()

    # 删除宝宝睡眠记录


def delete_baby_sleep_record(record_id):
    with connect() as conn:
        with conn.cursor() as cursor:
            cursor.execute("DELETE FROM baby_sleep_records WHERE id = %s", (record_id,))
        conn.commit()